package edu.toronto.whimper.DataProcessor;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import edu.toronto.whimper.Utils;
import edu.toronto.whimper.GeneralObjects.MyLocation;
import edu.toronto.whimper.GeneralObjects.NoiseSample;
import edu.toronto.whimper.GeneralObjects.Range;
import android.database.Cursor;

public class DBManager {

	public static boolean insertNoiseSample(DBAdapter db, String datetime, double noise, double latitude, double longitude, double altitude)
    {
    	db.open();      
        
    	long id = db.insertNoiseSample(datetime, noise, latitude, longitude, altitude);
        if(id == -1) return false;
        
        db.close();
        return true;            	
    }
	
	public static boolean insertDumpNoiseSample(DBAdapter db)
    {
    	//---add dump data---
        db.open();      
        
        db.insertNoiseSample(Utils.getNowDateTime(), 40, 43.664804, -79.394803, 0.0);
        db.insertNoiseSample(Utils.getNowDateTime(), 50, 43.664494, -79.396359, 0.0);
        db.insertNoiseSample(Utils.getNowDateTime(), 60, 43.664067, -79.398365, 0.0);
        db.insertNoiseSample(Utils.getNowDateTime(), 70, 43.662980, -79.397957, 0.0);
        db.insertNoiseSample(Utils.getNowDateTime(), 80, 43.662165, -79.397657, 0.0);
        /*
        ArrayList<MyLocation> l = Utils.getdumpLocations();
        
        double noise = 20;
        for (MyLocation var : l)
        {
        	long id = db.insertNoiseSample(Utils.getNowDateTime(), noise, (double)var.getLatitude() , (double)var.getLongitude(), 0.0);
        	if(id == -1) return false;
        	noise = noise *2;
        }
        */
        db.close();
        return true;            	
    }  
	// change algorithm considering that hours are in military time
	public static ArrayList<NoiseSample> getNoiseSampleByRange(ArrayList<NoiseSample> samples, Range range)
	{
		ArrayList<NoiseSample> results = new ArrayList<NoiseSample>();
		
		for (NoiseSample sample : samples) 
    	{
    		String dateSt = sample.getDate();	
    		
    		Date date = Utils.getDateTimeFromStr(dateSt,new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"));
    		
    		int hour = date.getHours();
    		
    		if(hour >= range.getFrom() && hour < range.getTo())
    		{
    			results.add(sample);
    		}    		    	  
    	}
		
		return results;
		
	}
	
	public static ArrayList<NoiseSample> getAllNoiseSamples(DBAdapter db)
    {
    	//---get all
	    db.open();
	    Cursor c = db.getAllNoiseSamples();  
	    
	    ArrayList<NoiseSample> noises = new ArrayList<NoiseSample>(c.getCount());
    	
	    if(c.getCount()>0)
    	{
	    	if (c.moveToFirst())
		    {
		        do {   
		        	String date = c.getString(1);
		        	double noise = c.getDouble(2);
		        	double lati = c.getDouble(3);
		        	double longi = c.getDouble(4);
		        	//double alti = c.getDouble(5);
		        	
		        	noises.add(new NoiseSample(date, noise, new MyLocation(lati, longi)));
		            
		        } while (c.moveToNext());
		        		        
		    }
    	}
	    else 
    	{
	    		        
    	}
    	
	    db.close();
	    
	    return noises;
    }     

}
